angular.module('BatchBind')
    .controller('BatchBind.BatchBindCtrl', ['$scope', '$state', 'Http', 'imanDialog', 'imanUsbPort', 'DevicePort', 'BatchBind.service',
        function ($scope, $state, Http, imanDialog, imanUsbPort, DevicePort, bindService) {
            $scope.data = {
                laserId: '',
                size: '',
                appearance: '',
                Barcode: '',
                lotNum: '',
                sizeTypeListId: 2,
                appearanceTypeListId: 2
            };

            bindService.setLotNumber({});

            var hasScan = false,
                toast;

            $scope.showToast = function (styleMes) {
                if (hasScan) {
                    return;
                }
                if (toast) {
                    return;
                }
                toast = imanDialog.toast({
                    message: styleMes,
                    block: true,
                    position: 'top',
                    duration: false,
                    backdropClickToClose: false,
                    hardwareBackButtonClose: false
                });
            };

            $scope.hideToast = function () {
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            };
            $scope.$on('$ionicView.afterEnter', function () {
                $scope.data.lotNum = bindService.getLotNumber().LotNumber;
                $scope.showToast('请扫描单品信息');
                imanUsbPort.start();
                DevicePort.open({
                    device: 0,
                    baudrate: 115200,
                    parseFn: true
                });
                // getQcInfo('1000022');
            });

            $scope.$on('imanUsbPort.input', function (e, inputStr) {
                $scope.data.laserId = inputStr;
                $scope.$apply();
            });
            $scope.$on('devicePort.DataReceived', function (e, barcode) {
                $scope.data.Barcode = barcode;
                getQcInfo($scope.data.Barcode);
            });

            $scope.$on('$ionicView.beforeLeave', function () {
                imanUsbPort.stop();
                DevicePort.close();
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            });

            $scope.submit = function () {
                if (!$scope.data.Barcode || $scope.data.Barcode === '') {
                    imanDialog.alert('请扫描芯片', '信息不全');
                    return;
                }
                imanDialog.showLoading();
                Http.post({
                    url: '/api/barcode/MOLotNumberToAssemblyBarcode',
                    data: {
                        Parameter: {
                            Barcode: $scope.data.Barcode,
                            SecurityId: $scope.data.laserId,
                            lotNum: $scope.data.lotNum,
                            Size: $scope.data.size,
                            appearance: $scope.data.appearance,
                            CompletePlan: bindService.getLotNumber()._id
                        }
                    }
                }).then(function () {
                    imanDialog.hideLoading();
                    imanDialog.toastBottom("绑定成功");
                })['finally'](function () {
                    hasScan = false;
                    $scope.showToast('请扫描单品信息');
                    imanDialog.hideLoading();
                });
            };

            function getQcInfo(Barcode) {
                imanDialog.showLoading();
                Http.post({
                    url: '/api/barcode/getBarcodeMaster',
                    data: {
                        Parameter: {
                            Barcode: Barcode
                        }
                    }
                }).then(function (info) {
                    $scope.data.barcode = info;
                    if (info._id) {
                        Http.post({
                            url: '/api/barcode/findAssemblyRelationsByBarcodeMaster',
                            data: {
                                Parameter: {
                                    barcodeMasterId: info._id
                                }
                            }
                        }).then(function (assemblyRelations) {
                            if (assemblyRelations.AssemblyBarcode) {
                                $scope.data.laserId = assemblyRelations.AssemblyBarcode.Barcode;
                            } else {
                                $scope.data.laserId = '';
                            }
                        }, function () {
                            $scope.data.laserId = '';
                        });
                    } else {
                        $scope.data.laserId = '';
                    }
                    hasScan = true;
                    $scope.hideToast();
                })['finally'](function () {
                    imanDialog.hideLoading();
                });
            }

            $scope.goMODetail = function () {
                $state.go('MODetail');
            };

            //选择信息
            $scope.showSelectDialog = function (obj) {
                Http.post({
                    url: '/api/Judge/getExtendEnum',
                    data: {
                        Parameter: {
                            Code: obj.Code
                        }
                    }
                }).then(function (info) {
                    var typeArr = [];
                    angular.forEach(info, function (value, key) {
                        typeArr.push({id: key, name: value});
                    });
                    imanDialog.showList({
                        title: obj.Title,
                        list: typeArr,
                        valueField: 'id',
                        displayField: 'name',
                        value: (function () {
                            var listId;
                            if ('SizeType' === obj.Code) {
                                listId = $scope.data.sizeTypeListId ? $scope.data.sizeTypeListId : 2;
                            } else if ('Appearance' === obj.Code) {
                                listId = $scope.data.appearanceTypeListId ? $scope.data.appearanceTypeListId : 2;
                            }
                            return listId;
                        })()
                    }).then(function (select) {
                        if ('SizeType' === obj.Code) {
                            $scope.data.size = select.name;
                            $scope.data.sizeTypeListId = select.id;
                        } else if ('Appearance' === obj.Code) {
                            $scope.data.appearance = select.name;
                            $scope.data.appearanceTypeListId = select.id;
                        }

                    });
                });
            };
            
        }
    ]);
